<#--
	@Dependence bscommon
-->
<#assign tag = fn.tag>
<#macro container name='' namePrefix='' label='' vertical=true freeModel=false columnCount=1 clazz='' style='' bgImage=''
	minWidth=0 labelWidth=0 itemWidth='' colspan=1 model='' positionCol=-1 id=''>
	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<#local cid = tag.getCid(name, 'container')>
	<#assign _namePrefix = tag.pushNamePrefix(action, namePrefix)>
	<#assign pminWidth=minWidth>
	
	<#-- push ps -->
	<#local shortName = tag.getShortName(name)>
	<#local _ps = ps>
	<#assign ps = ps.checkAddElement(shortName, name, 'container', {})>
	<#if _ps.length?? && _ps.length gt 0>
		<#assign pminWidth=_ps.length>
	</#if>
	
	<#local headHeight = 0>
	<#if label!=''>
		<#local headHeight = 32>
	</#if>
	
	<div class="stdEle stdCtn container panel panel-default ${clazz}" <#if style !=''> style='${style}'</#if> name="${shortName}" fullName="${name}" uiType="container" vertical="true">
	  <div class="panel-heading"><@i18n name=label/></div>
	  <div class="panel-body form-horizontal ctnEle">
	  	<#if columnCount == 100>
	  		<#nested>
	  	<#else>
		  	<#local oldGridModel = _gridModel!0>
		  	<#assign _gridModel = 7>
		  	<#local colWidth = 100 / columnCount>
		  	<#list 0..(columnCount-1) as no>
		  	<div style="float:left;width:${colWidth}%" class="colEle">
		  		<#assign _positionCol = no>
		    	<#nested>
		    </div>
		    </#list>
		    <#assign _gridModel = oldGridModel>
		</#if>
	  </div>
	</div>
	
	<#assign ps = _ps>
	<#assign _namePrefix = tag.popNamePrefix(action, namePrefix)>
</#macro>

<#macro row name='' clazz='' style='' positionCol=-1 id=''>
	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<div fullName="${name}" class="stdEle stdCtn ctnEle row ${clazz}" <#if style !=''> style='${style}'</#if> uiType='row'>
		<#nested>
	</div>
</#macro>

<#macro column name='' clazz='col-md-1' style='' positionCol=-1 id=''>
	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<div fullName="${name}" class="stdEle stdCtn ctnEle ${clazz}" <#if style !=''> style='${style}'</#if> uiType='column'>
		<#nested>
	</div>
</#macro>

<#macro tab name='' namePrefix='' labels=[] urls=[] selected=-1 vertical=false clazz='' style='' enableClose=false cache=true model='' positionCol=-1 id=''>
	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<ul class="stdEle stdCtn nav nav-tabs ${clazz}" role="tablist" fullName='${name}' <#if style !=''> style='${style}'</#if> uiType='tab'>
	  <li role="presentation" class="active"><a href="#">Home</a></li>
	  <li role="presentation"><a href="#">Profile</a></li>
	  <li role="presentation"><a href="#">Messages</a></li>
	</ul>
</#macro>

<#macro tabpage name='' namePrefix='' label='' url='' clazz='' style='' enableClose=false model='' positionCol=-1 id=''>
	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<#assign _namePrefix = tag.pushNamePrefix(action, namePrefix)>
	<#--TODO: enable pElement? -->
	
	<#if url == '' || ps.isMock()>
		<#local tabpageId = '_tab_' + fn.id>
		<#local linkWithBase = '#' + tabpageId>
	<#else>
		<#local linkWithBase = tag.formatLink(url, ps, true)>
		<#-- TODO: add the ps of the link page to current ps? -->
	</#if>
	
	<#if linkWithBase != ''>
		<#local shortName = tag.getShortName(name)>
		<li<#if id!=''> id="${id}"</#if> name='${shortName}' fullName='${name}' 
			class="tabHeaderTitle stdEle<#if enableClose> withCloseLink</#if>" style='${style}' uiType='tabpage'>
			<a href="${linkWithBase}" label="<@i18n name=label/>" class="title label"><@i18n name=label/></a>
			<#if enableClose>
				<a href='javascript:void(0)' onclick='closeTab(this)' class="closeLink"><span class='ui-icon ui-icon-close'><@i18n name='commons.closeTab'/></span></a>
			</#if>
		</li>
	</#if>
	
	<#if tabpageId?exists>
		<div class="j-hide-display stdCtn jcontainer-body" fullName='${name}' id='${tabpageId}' uiType='tabpage'>
			<#nested>
		</div>
	<#else>
		<#nested>
	</#if>
	
	<#assign _namePrefix = tag.popNamePrefix(action, namePrefix)>
</#macro>

<#macro pageinfo name='' namePrefix='' path='' sort='' dir='' clazz='' style='' model='' collection=[] minWidth=0 paginationClazz=''
	rowClickLinks=[] dblClick='_updateRecord' dblClickPath='' showPagination=true enableSearch=true useWinScrollBar=true showWinBtn=true positionCol=-1 id=''>
	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<#local cid = tag.getCid(name, 'pageinfo')>
	<#assign _namePrefix = tag.pushNamePrefix(action, namePrefix)>
	<#assign pminWidth = minWidth>

	<#-- push ps -->
	<#local shortName = tag.getShortName(name)>
	<#local _ps = ps>
	<#assign ps = ps.checkAddElement(shortName, name, 'pageinfo', {'model':model, 'sort':sort, 'dir':dir})>
	
	<#if ps.length?? && ps.length gt 0>
		<#assign pminWidth = ps.length>
	</#if>
	
	<#-- get pageInfo -->
	<#if collection?size == 0>
		<#assign pageInfo = fn.val(action, _namePrefix)!>
	<#else>
		<#assign pageInfo = collection>
	</#if>
	<#if !pageInfo?? || !pageInfo.totalCount??>
		<#if pageInfo?? && pageInfo?is_collection>
			<#assign pageInfo=fn.newModel('com.googlecode.cswish.model.PageInfo',{'data',pageInfo})>
		<#else>
			<#assign pageInfo=fn.newModel('com.googlecode.cswish.model.PageInfo',{'data',[]})>
		</#if>
	</#if>
	
	<#-- check permission -->
	<#local service = fn.service('loginUser')>
	<#local dblClick = ps.extraParams?if_exists.dblClick!dblClick>
	<#local dblClickPath = fn.linkNoBase(dblClickPath,true)>
	<#local hasPermission = dblClickPath != '' && dblClickPath != '#' && service.hasLinkPermission(dblClickPath, false)>
	<#if !hasPermission>
		<#local dblClick = ''>
	<#else>
		<#local dblClickRecords = service.getRecordPermission(dblClickPath, false, pageInfo.data![])>
	</#if>
	<#local rowClickLinks = fn.linkNoBases(rowClickLinks,true)>
	<#local clickLinks = service.filterLinkPermission(rowClickLinks, true)>
	
	<#-- show the table -->
	<#if path == ''>
		<#local submitAction = fn.getSubmitAction(actionPath)>
		<#local path = fn.linkNoBase(submitAction,true) + '?'>
	<#elseif path?index_of('?') < 0>
		<#local path = fn.linkNoBase(path,true) + '?'>
	</#if>
	<#local tableWidth = fn.tag.css(pminWidth, style, 'width')>
<div id="${id}" class="stdEle stdCtn ${clazz} ${cid}" uiType="pageinfo" style="${style}" name="${shortName}" fullName="${name}"
		 sort="${fn.s(fn.val(action, 'sort'),'sort')!}" dir="${fn.s(fn.val(action, 'dir'),'dir')!}" showWinBtn="${showWinBtn?string}"
		pageSize='${pageInfo.pageSize}' totalCount='${pageInfo.totalCount}' maxPageNo='${pageInfo.maxPageNo}' namePrefix="${_namePrefix}"
		<#if path!=''>path="${base}${path}&_resultCode=$element&_elementName=${name}<#if Parameters._tpl?exists>&_tpl=${Parameters._tpl}</#if>"</#if>
		<#if dblClick != ''>dblClickPath="${base}${dblClickPath}"</#if>
		<#if clickLinks?size gt 0>links="${tag.getPageInfoClickLink(base,clickLinks,'|;|')}" model="${model}"</#if>>
	<#if showPagination>
		<@pagination pageInfo=pageInfo paginationClazz=paginationClazz/>
	</#if>
	
	<#-- provide default value for better display performance, see jtaglig.js, _adjustTableHeight-->
	<#if pageInfo.data?? && pageInfo.data?size gt 10>
		<#if clickLinks?size gt 1>
			<#local pageinfoListHeight = 235>
		<#elseif clickLinks?size == 1>
			<#local pageinfoListHeight = 350>
		</#if>
	</#if>
	
	<#local oldGridModel = _gridModel!0>
	<#assign _gridModel=9>
	<@span name="_selectButton" width=25/>
	<#nested>
	
	<#assign _emptyColWidthCount = tag.getEmptyColWidthCount(action, ps)>
	<#assign _colCount = tag.getColCount(action, ps)>
	<#assign _totalColWidth = tag.getTotalColWidth(action, ps)>
	<#assign _colIndex = -1>
	
	<table class="table table-striped">
		<thead>
		<tr>			
			<#-- don't show the sort flag, show the nested content -->
			<#assign _gridModel=42>
			<@span name="_selectButton" clazz="fixedEle selectAll" width=25><input type="checkbox" onclick="selectAll(this)"/></@span>
			<#-- sort condition: sort path is not blank & data exists -->
			<#if path != '' && pageInfo.data?? && pageInfo.data?size gt 0>
				<#-- show the sort flag, show the readonly text -->
				<#assign _gridModel=4>
			<#else>
				<#-- don't show the sort flag, show the readonly text -->
				<#assign _gridModel=41>
			</#if>
			<#nested>
		</tr>
		</thead>
		
		<#if pageInfo.data?? && pageInfo.data?size gt 0>
		<tbody>
			<#assign _gridModel=5>
			
			<#local pageData = pageInfo.data>
			<#list pageData as item>
				<#assign _gridItem=item>
				<#assign thePrefix = 'results[' + item_index + '].'>
				<#assign modelId = fn.val(item, 'id')!0>
				<#local hasRecordPermission = hasPermission && (dblClickRecords == pageData || dblClickRecords.indexOf(item) >= 0)>
				<tr <#if item_index % 2 == 1>class="even"</#if> modelId="${modelId}" <#if !hasRecordPermission>disableDblClick=true</#if>>
					<@span name="_selectButton">
						<input type=checkbox name="${thePrefix}id" value="${modelId}" onclick="stopEvent(event)" ondblclick="stopEvent(event)"/>
					</@span>
					<#if !pshow><input type=hidden name="${thePrefix}id" value="${modelId}"/></#if>
					
					<#nested>
				</tr>
			</#list>
		</tbody>
		<#else>
		<tbody><tr class="noData"><td colspan="${_colCount}"><@i18n name='pageinfo.noData'/></td></tr></tbody>
		</#if>
	</table>
</div>
	<#assign _gridModel=oldGridModel>
	<#-- popup ps -->
	<#assign ps = _ps>
	
	<#assign _namePrefix = tag.popNamePrefix(action, namePrefix)>
</#macro>

<#macro datagrid name='' namePrefix='' collection=[] required=false sortable=false autoStatNumber=false stats=[]
 showAddCarBtn=false clazz='' style='' model='' width=0 label='' positionCol=-1 id=''>
 	<#if _gridModel?? && _gridModel == 7 && positionCol != _positionCol>
		<#return>
	</#if>
	
	<#local cid = tag.getCid(name, 'datagrid')>
	<#assign _namePrefix = tag.pushNamePrefix(action, namePrefix)>
	
	<#-- push ps -->
	<#local shortName = tag.getShortName(name)>
	<#local _ps = ps>
	<#assign ps = ps.checkAddElement(shortName, name, 'datagrid', {'model':model})>
	
	<#local oldGridModel = _gridModel!0>
	<#assign _gridModel=9>
	<@span name="_removeButton" width=25/>
	<#nested>
	
	<#local minWidth = tag.getTableWidth(ps)>
	<#if minWidth gt width>
		<#assign pwidth=minWidth>
	<#else>
		<#assign pwidth=width>
	</#if>
	<#if ps.length?? && ps.length gt minWidth>
		<#assign pwidth=ps.length>
	</#if>
	
	<div class="stdEle stdCtn" name="${shortName}" fullName="${name}" uiType="datagrid">
	<#if label != ''>
	<div class="jdatagrid-label"><li>${label}</li><span><span></div>
	</#if>
	<table id="${id}" class="table ${clazz} ${cid}"<#if pwidth gt 0 || style!=''> style="${style}"</#if> namePrefix="${_namePrefix}"<#if required> required="true"</#if>>
	<thead>
		<tr>
			<#assign _gridModel=11>
			<@span name="_removeButton" clazz="fixedEle" width=15 required=required><#if showAddCarBtn><span onclick="showCarItem(this)" class="shoppingCar">Add</span></#if></@span>
			<#assign _gridModel=1>
			<#nested>
		</tr>
	</thead>
	<tbody>
		<#assign _gridModel=2>
		<tr class="templateRow">
			<@span name="_removeButton" clazz="removeColumn"><em></em><a href="javascript:void(0)" class="removeLink" onclick="removeRow(this)">X</a></@span>
			<#nested>
		</tr>
		
		<#assign _gridModel=3>
		<#if collection?size == 0>
			<#local collection=fn.val(action, _namePrefix)![]>
		</#if>
		<#if collection?is_collection || collection?is_sequence>
		<#list collection as item>
			<#assign _gridItem=item>
			<tr>
				<@span name="_removeButton" clazz="removeColumn">
					<em></em><a href="javascript:void(0)" class="removeLink" onclick="removeRow(this)">X</a>
				</@span>
				<@hidden name="id" value=fn.val(item, "id")!/>
				<#nested>
			</tr>
		</#list>
		</#if>
	</tbody>
	<#local stats = tag.getStatProperties(action, ps, stats, autoStatNumber)>
	<#local showStat = stats?size gt 0>
	<#if showStat>
		<tfoot><tr>
			${tag.formatTableStat(action, ps, collection, stats)}
		</tr></tfoot>
	</#if>
	</table>
	</div>
	<@script name=cid _inner=true>
	when('BS.initTableGridEvent', function(){
		BS.initTableGridEvent($('.${cid}'),${required?string},${sortable?string},${fn.json(stats)});
	});
	</@script>
	<#assign _gridModel=oldGridModel>
	<#-- popup ps -->
	<#assign ps = _ps>
	
	<#assign _namePrefix = tag.popNamePrefix(action, namePrefix)>
</#macro>

<#macro span name value='' label='' textModel=false required=false show=true clazz='' style='' width=0 colspan=1 id=''>
	<@control type='span' id=id name=name value=value width=width label=label clazz=clazz style=style required=required colspan=colspan show=show textModel=textModel>
		<#nested>
	</@control>
</#macro>


<#assign tmp = ps.safeContext().addCssCategory('bscommon')>